package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.NodeMapper;
import com.jd.finance.core.pojo.dto.AdminListDTO;
import com.jd.finance.core.pojo.entity.Node;
import com.jd.finance.core.service.NodeService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 菜单表(权限表) 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-20
 */
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService {

    @Resource
    private NodeMapper nodeMapper;
    @Resource
    private AdminUtilsServiceImpl adminUtilsService;
    @Resource
    private OpreateLogService opreateLogService;

    /**
     * 根据uid查询菜单
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResult getAdminList(Integer uid) {
        if (uid == null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }
        List<AdminListDTO> adminListDTO = nodeMapper.selectNodeInfo(uid);
        if (adminListDTO == null) {
            log.error("未查询到菜单信息!");
            return BaseResult.error().message("未查询到菜单信息!");
        }

        return BaseResult.ok().data("node", adminListDTO);
    }


    /**
     * 查看所有节点信息
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResult selectNodeInfo(Integer uid) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }

        List<Node> nodes = nodeMapper.selectList(null);
        return BaseResult.ok().data("nodes", nodes);
    }



    /**
     * 添加节点
     *
     * @param uid
     * @param listName
     * @param listUrl
     * @param request
     * @return
     */
    @Override
    public BaseResult addNode(Integer uid, String listName, String listUrl, HttpServletRequest request) {
        if (uid == null) {
            log.error("参数为空S");
            return BaseResult.error().message("参数为空S");
        }
        if (StringUtils.isAllBlank(listName, listUrl)) {
            log.error("参数为空S");
            return BaseResult.error().message("参数为空S");
        }
        if (!adminUtilsService.isHR(uid)) {
            log.error("无访问权限S");
            return BaseResult.error().message("无访问权限");
        }
        Node node = new Node();
        node.setListUrl(listUrl);
        node.setListName(listName);
        int insert = nodeMapper.insert(node);
        boolean add = opreateLogService.add(uid, "添加", "添加节点", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }

        return BaseResult.ok().data("tianjiajieguo", insert);
    }
}